<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AutoMR</title>
</head>
<body>
    <b>form:</b><input type="text" name="urlForm" placeholder="请输入form" value="ABCD"><br>
    <b>nums:</b><input type="text" name="urlNums" placeholder="请输入次数" value=3><br>
    <b>times:</b><input type="text" name="urlTimes" placeholder="请输入时间间隔" value=5000><br>
    <button id="startBtn">开始</button><br>
    <textarea id="logBox" cols="30" rows="10"></textarea>
</body>
<script>
    const inputElements = document.getElementsByTagName('input');
var urlForm;
var urlNums;
var urlTimes;
var urlText = "https://cn.bing.com/search?q=%%%&form=" + urlForm;

var urlList = [];

document.getElementById("startBtn").onclick=function(){
    startFun();
};

function addLog(log,logText){
    const logBox = document.getElementById("logBox");
    logBox.append(log + ":" + logText + '\n');
}


function getRandomCNcode(length){
    function randomAccess(min,max){
        return Math.floor(Math.random()* (min - max) + max);
    }
    let name ="";
    
    for(let i = 0;i < length; i++){
        name = name + String.fromCharCode(randomAccess(0x4E00,0x9FA5));
    }
    console.log("name",name);
    return name;
}

// 打开新页面


function startFun(){
    urlForm = inputElements[0].value;
    urlNums = inputElements[1].value;
    urlTimes = inputElements[2].value;
    addLog("urlForm",urlForm);
    addLog("urlNums",urlNums);
    console.log(urlForm);
    console.log(urlNums);
    for (let i = 0; i < urlNums; i++) {
        var length = Math.floor(Math.random()*10)+1;
        urlList.push(urlText.replace("%%%",getRandomCNcode(length)));
    }

    var i=0;
    var ww; 
    ww = window.open(urlList[i],"_blank","scrollbars=yes,resizable=1,modal=false,alwaysRaised=yes");
    console.log("open:",i);
    addLog("open",i);
    var interval;
    interval = setInterval(function(){
        i++;
        ww.close();
        console.log("closed:",i-1);
        addLog("closed",i-1);
        if(i<urlList.length){
            ww = window.open(urlList[i],"_blank","scrollbars=yes,resizable=1,modal=false,alwaysRaised=yes");
            console.log("open:",i);
            addLog("open",i);
        }else{
            clearInterval(interval);
        }
    },urlTimes)    
}


</script>
</html>